package com.yoso.shop.infrastructure.base.controller;

import com.yoso.shop.infrastructure.constants.base.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author shizejin
 * @date 2021/11/2
 * @description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseResponse<T> {
    private String code;
    private T data;
    private String message;
    private boolean result = true;

    public BaseResponse(String code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }
    public static BaseResponse<Void> error(String msg){
        return new BaseResponse<>(ResponseCode.ERROR, null, msg, false);
    }
    public static BaseResponse<Void> success(String msg){
        return new BaseResponse<>(ResponseCode.SUCCESS,null,msg);
    }
    public static BaseResponse<Object> success(String msg,Object obj){
        return new BaseResponse<>(ResponseCode.SUCCESS,obj,msg);
    }
    public static BaseResponse<Void> result(int result){
        if (result == 1){
            return new BaseResponse<>(ResponseCode.SUCCESS,null,"操作成功");
        }
        return new BaseResponse<>(ResponseCode.ERROR,null,"操作失败",false);
    }
    public boolean isSuccess(){
        return this.result;
    }
}
